/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import mrp.productionPlanning.entity.HistoricalDataEntity;

/**
 *
 * @author GKnight
 */
@Stateless
@LocalBean
public class HistoricalDataSessionBean {

    @PersistenceContext
    private EntityManager em;
    
    public HistoricalDataEntity createHistoricalData(String monthName, int yearName){
        HistoricalDataEntity historicalData = new HistoricalDataEntity();
        historicalData.setMonthName(monthName);
        historicalData.setYearName(yearName);
        em.persist(historicalData);
        return historicalData;
    }
    
    public HistoricalDataEntity getHistoricalDataByMonth(String monthName,int yearName){
        Query query = em.createQuery("SELECT h FROM HistoricalDataEntity h WHERE h.monthName = :monthName AND h.yearName = :yearName ");
        query.setParameter("monthName", monthName);
        query.setParameter("yearName", yearName);
        List<HistoricalDataEntity> historicalDatas = query.getResultList();
        HistoricalDataEntity hd = historicalDatas.get(0);
        return hd;
    }
    
    public List<HistoricalDataEntity> getHistoricalDataByYear(int yearName){
        Query query = em.createQuery("SELECT h FROM HistoricalDataEntity h WHERE h.yearName = :yearName");
        query.setParameter("yearName", yearName);
        List<HistoricalDataEntity> historicalDatas = query.getResultList();
        return historicalDatas;
    }
    
    public List<HistoricalDataEntity> getAllHistoricalData(){
        Query query = em.createQuery("SELECT h FROM HistoricalDataEntity h");
        List<HistoricalDataEntity> historicalDatas = query.getResultList();
        return historicalDatas;
    }
    
}
